import traceback


from fastapi import APIRouter, Depends

from app.api.response import response_docs, Response
from app.api.status import Status
from app.business.model_config import ModelConfigListBiz, ModelConfigBiz

from app.datatype.model_config import ModelConfigListMdl

from app.initializer import g
from app.middleware.auth import get_current_user, JWTUser

model_config_router = APIRouter()
_active = True  # 激活状态


@model_config_router.post(
    path="/getModelConfigList",
    summary="获取model列表",
    responses=response_docs(
        model=ModelConfigListMdl,
        is_listwrap=True,
    ),
)
async def lst(
        model_biz: ModelConfigListBiz,
        current_user: JWTUser = Depends(get_current_user),  # 认证
):
    try:
        data = await model_biz.lst()
        if not data:
            return Response.failure(msg="未匹配到记录", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        return Response.failure(msg="获取知识库列表失败", error=e)
    return Response.success(data=data)


@model_config_router.post(
    path="/createModelConfig",
    summary="创建model配置",
    responses=response_docs(data={
        "id": int,
    }),
)
async def create(
        model_biz: ModelConfigBiz,
        current_user: JWTUser = Depends(get_current_user),
):
    try:

        model_id = await model_biz.create()
        if not model_id:
            return Response.failure(msg="创建失败", status=Status.RECORD_EXISTS_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="创建失败", error=e)
    return Response.success(data={"id": model_id})


@model_config_router.put(
    path="/updateConfig",
    summary="更新model配置",
    responses=response_docs(data={
        "id": int,
    }),
)
async def update(
        model_biz: ModelConfigBiz,
        current_user: JWTUser = Depends(get_current_user),
):
    try:
        model_id = await model_biz.update()
        if not model_id:
            return Response.failure(msg="更新失败", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="更新失败", error=e)
    return Response.success(data={"id": model_id})


@model_config_router.delete(
    path="/deleteConfig",
    summary="删除model配置",
    responses=response_docs(data={
        "id": int,
    }),
)
async def update(
        model_biz: ModelConfigBiz,
        current_user: JWTUser = Depends(get_current_user),
):
    try:
        model_id = await model_biz.delete()
        if not model_id:
            return Response.failure(msg="删除失败", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="删除失败", error=e)
    return Response.success(data={"id": model_id})


# @model_config_router.get(
#     path="/getCode",
#     summary="获取机器码",
#     responses=response_docs(data={
#         "code": str,
#     }),
# )
# async def get_code(
# ):
#     try:
#         hardware_collector = HardwareInfoCollector()
#         print("硬件信息收集器已创建", hardware_collector)
#         # 2. 生成机器码
#         machine_code_generator = MachineCodeGenerator("rag-core", hardware_collector)
#         machine_code = machine_code_generator.generate_machine_code()
#         print(f"机器码: {machine_code}")
#     except Exception as e:
#         g.logger.error(traceback.format_exc())
#         return Response.failure(msg="获取机器码失败", error=e)
#     return Response.success(data={"code": machine_code})
#
#
# @model_config_router.post(
#     path="/setAuthorization",
#     summary="项目授权",
#     responses=response_docs(
#         data={
#             "code": '',
#         }
#     ),
# )
# async def get_model_config_route(
#         code: CodeAuthorizationMdl,
# ):
#     # try:
#     hardware_collector = HardwareInfoCollector()
#     print("硬件信息收集器已创建", hardware_collector)
#     # 2. 生成机器码
#     machine_code_generator = MachineCodeGenerator("rag-core", hardware_collector)
#     machine_code = machine_code_generator.generate_machine_code()
#     print(f"机器码: {machine_code}")
#
#     # # 3. 验证授权
#     license_verifier = LicenseVerifier(machine_code_generator)
#     license_code = code.authorization_code
#     private_key = code.private_key
#     is_licensed = license_verifier.validate_license_with_key(license_code, private_key)
#     if not is_licensed:
#         return Response.failure(msg="授权失败", status=Status.UNAUTHORIZED_ERROR)
#
#     # except Exception as e:
#     #     return Response.failure(msg="授权失败", error=e)
#
#
#     return Response.success(data={"code": '授权成功'})
